package com.hsy.swu.version

import android.animation.ObjectAnimator
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.animation.AccelerateInterpolator
import com.hsy.swu.R
import com.hsy.swu.databinding.ActivityVersionBinding
import com.hsy.swu.hsyutils.HsyActivity
import com.hsy.swu.hsyutils.HsyViewModel

class VersionActivity : HsyActivity<ActivityVersionBinding, HsyViewModel>() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        startTvNameMaskAnimation()
        mBinding.tvVersionName.text = "version_${Updater.currentVersionName}"

        Updater(this@VersionActivity).let {
            if (it.checkIsNeedUpdate()) {
                mBinding.tvVersionTip.apply {
                    setBackgroundResource(R.drawable.bg_button)
                    text = "有新版本，点击更新"
                    setTextColor(Color.WHITE)
                    setOnClickListener { _ ->
                        it.showUpdateDialog(false)
                    }
                    visibility = View.VISIBLE
                }
            } else {
                mBinding.tvVersionTip.text = "已是最新版"
            }
        }
    }

    private fun startTvNameMaskAnimation() {
        mBinding.tvAppName.post {
            val width = mBinding.tvAppName.width.toFloat()
            val animator: ObjectAnimator = ObjectAnimator.ofFloat(
                mBinding.imgNameMask,
                "translationX",
                -width * 1.5f,
                width * 1.5f
            ).apply {
                duration = 3000
                interpolator = AccelerateInterpolator()
                repeatCount = ObjectAnimator.INFINITE
            }
            animator.start()
        }
    }
}